package src;

import it.sauronsoftware.jave.Encoder;
import org.mortbay.util.ajax.JSON;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhikang.shu
 * @date 2021/3/19
 * @desc TODO add description in here
 */
public class TestGetVideoInfo {


    public static void main(String[] args) {
        System.out.println(JSON.toString(getVideoInfo()));
    }




    private static Map getVideoInfo() {
        File file = new File("D:\\24fSrK.mp4");
        Map map = new HashMap();
        it.sauronsoftware.jave.Encoder encoder = new Encoder();
        try {
            it.sauronsoftware.jave.MultimediaInfo m = encoder.getInfo(file);
            long duration = m.getDuration();
            long secondDuration = duration / 1000;
            map.put("secondDuration", secondDuration);//获取视频秒数
            map.put("format", m.getFormat());//获取视频格式
            map.put("width", m.getVideo().getSize().getWidth());//获取视频宽高
            map.put("height", m.getVideo().getSize().getHeight());//获取视频长高
            FileInputStream fis = new FileInputStream(file);
            FileChannel fc = fis.getChannel();
            BigDecimal fileSize = new BigDecimal(fc.size());
            String size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) +         "MB";
            map.put("size", size);//获取视频大小
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            file.delete();
        }
        return map;
    }
}
